﻿#include"ProductView.h"
#include"XQAlgorithm.h"
#include<QLineEdit>
#include<QComboBox>
#include<QBoxLayout>
#include<QPushButton>
#include<QLabel>
#include<QSpinBox>
#include<QRegularExpressionValidator>
#include<QDoubleValidator>
#include<QDateTimeEdit>
#include<QCompleter>
#include<QMessageBox>
ProductView::ProductView(QWidget* parent)
	:XQOperateView(parent)
{

}

ProductView::~ProductView()
{
}

void ProductView::setReadOnly(bool readOnly)
{
	for (int i = (int)Type::OkButtonType; i <= (int)Type::CtimeType; i++)
	{
		setReadOnly(i, readOnly);
	}
}

void ProductView::setReadOnly(int type, bool readOnly)
{
	//XQOperateView::setReadOnly(type,readOnly);
	switch ((ProductView::Type)type)
	{
	case ProductView::Type::OkButtonType:
		transcribeWidget((int)XQOperateView::Type::OkButtonType)->setEnabled(!readOnly);
		break;
	case ProductView::Type::CloseButtonType:
		transcribeWidget((int)XQOperateView::Type::CloseButtonType)->setEnabled(!readOnly);
		break;
	case ProductView::Type::IdType:m_idEdit->setReadOnly(readOnly);
		break;
	case ProductView::Type::NameType:m_nameEdit->setEnabled(!readOnly);
		break;
	case ProductView::Type::TypeType:m_typeEdit->setEnabled(!readOnly);
		break;
	case ProductView::Type::PriceType:m_priceEdit->setReadOnly(readOnly);
		break;
	case ProductView::Type::NumberType:m_numberEdit->setReadOnly(readOnly);
		break;
	case ProductView::Type::TotalPriceType:m_totalPriceEdit->setReadOnly(readOnly);
		break;
	case ProductView::Type::MtimeType:m_mtimeEdit->setEnabled(!readOnly);
		break;
	case ProductView::Type::CtimeType:m_ctimeEdit->setEnabled(!readOnly);
		break;
	default:
		break;
	}
}

ProductModel ProductView::productModel() const
{
	ProductModel model;
	model.setProduct_id(product_id());
	model.setName(name());
	model.setType(type());
	model.setPrice(price());
	model.setNumber(number());
	model.setCTime(ctime());
	model.setMTime(mtime());
	return model;
}

quint64 ProductView::product_id() const
{
	return m_idEdit->text().toULongLong();
}

QString ProductView::name() const
{
	return m_nameEdit->currentText();
}

QString ProductView::type() const
{
	return m_typeEdit->currentText();
}

double ProductView::price() const
{
	return m_priceEdit->text().toDouble();
}

quint64 ProductView::number() const
{
	return m_numberEdit->text().toULongLong();
}

double ProductView::totalPrice() const
{
	return m_totalPriceEdit->text().toDouble();
}

QDateTime ProductView::mtime() const
{
	return m_mtimeEdit->dateTime();
}

QDateTime ProductView::ctime() const
{
	return m_ctimeEdit->dateTime();
}

void ProductView::setProductModel(const ProductModel& model)
{
	setProduct_id(model.product_id());
	setName(model.name());
	setType(model.type());
	setPrice(model.price());
	setNumber(model.number());
	setTotalPrice(model.totalPrice());
	setMTime(model.mtime());
	setCTime(model.ctime());
}

void ProductView::setProduct_id(quint64 product_id)
{
	m_idEdit->setText(QString::number(product_id));
}

void ProductView::setName(const QString& name)
{
	if (m_nameEdit->findText(name) == -1)
		m_nameEdit->addItem(name);
	m_nameEdit->setCurrentText(name);
}

void ProductView::setType(const QString& type)
{
	if (m_typeEdit->findText(type) == -1)
		m_typeEdit->addItem(type);
	m_typeEdit->setCurrentText(type);
}

void ProductView::setPrice(double price, char format, int precision)
{
	m_priceEdit->setText(QString::number(price, format, precision));
}

void ProductView::setNumber(quint64 number)
{
	m_numberEdit->setValue(number);
}

void ProductView::setNumberRange(int minimum, int maximum)
{
	m_numberEdit->setRange(minimum, maximum);
}

void ProductView::setTotalPrice(double totalPrice, char format, int precision)
{
	m_totalPriceEdit->setText(QString::number(totalPrice, format, precision));
}

void ProductView::setMTime(const QDateTime& time)
{
	m_mtimeEdit->setDateTime(time);
}

void ProductView::setCTime(const QDateTime& time)
{
	m_ctimeEdit->setDateTime(time);
}

void ProductView::setNameCompleter(const QStringList& list)
{
	auto completer = m_nameEdit->completer();
	if (completer)
		completer->deleteLater();
	completer = new QCompleter(list, m_nameEdit);
	completer->setCaseSensitivity(Qt::CaseInsensitive); // 设置大小写不敏感
	completer->setCompletionMode(QCompleter::PopupCompletion); // 设置补全模式为弹出列表

	m_nameEdit->setCompleter(completer);
}

void ProductView::setNameItems(const QStringList& list)
{
	auto str = m_nameEdit->currentText();
	m_nameEdit->clear();
	m_nameEdit->addItems(list);
	m_nameEdit->setCurrentText(str);
}

void ProductView::setTypeCompleter(const QStringList& list)
{
	auto completer = m_typeEdit->completer();
	if (completer)
		completer->deleteLater();
	completer = new QCompleter(list,m_typeEdit);
	completer->setCaseSensitivity(Qt::CaseInsensitive); // 设置大小写不敏感
	completer->setCompletionMode(QCompleter::PopupCompletion); // 设置补全模式为弹出列表

	m_typeEdit->setCompleter(completer);
	//m_typeEdit->additem
}

void ProductView::setTypeItems(const QStringList& list)
{
	auto str = m_typeEdit->currentText();
	m_typeEdit->clear();
	m_typeEdit->addItems(list);
	m_typeEdit->setCurrentText(str);
}

void ProductView::init()
{
	XQOperateView::init();
	connect(m_priceEdit, &QLineEdit::textChanged, [=]{setTotalPrice(price()*number()); });
	connect(m_numberEdit, &QSpinBox::textChanged, [=] {setTotalPrice(price() * number()); });
	setCssFile(this, ":/XQWidget/style.css");
}

void ProductView::okButtonSlots()
{
	if (isVisible(Type::IdType) && m_idEdit->text().isEmpty())
	{
		QMessageBox::information(this, "提示", "编号不可为空");
		return;
	}
	if (isVisible(Type::NameType) && name().isEmpty())
	{
		QMessageBox::information(this, "提示", "名称不可为空");
		return;
	}
	if (m_nameEdit->findText(name()) == -1)
	{
		QMessageBox::information(this, "提示", "物品不在库");
		return;
	}
	if (isVisible(Type::TypeType) && type().isEmpty())
	{
		QMessageBox::information(this, "提示", "类型不可为空");
		return;
	}
	if (isVisible(Type::PriceType) && m_priceEdit->text().isEmpty())
	{
		QMessageBox::information(this, "提示", "价格不为空");
		return;
	}
	if (isVisible(Type::PriceType) && price()==0)
	{
		QMessageBox::information(this, "提示", "价格不能为零");
		return;
	}
	if (isVisible(Type::NumberType) && m_numberEdit->text().isEmpty())
	{
		QMessageBox::information(this, "提示", "数量不为空");
		return;
	}
	emit okSignals();
}

void ProductView::init_ui()
{
	XQWidget::init_ui();
	setCssFile(this, ":/XQUserInfoView/style.css");
	setWindowTitle("用户信息");
	//设置内容布局
	auto layout = new QBoxLayout(QBoxLayout::TopToBottom);
	layout->addWidget(init_id());
	layout->addWidget(init_name());
	layout->addWidget(init_type());
	layout->addWidget(init_price());
	layout->addWidget(init_number());
	layout->addWidget(init_totalPrice());
	layout->addWidget(init_mtimeEdit());
	layout->addWidget(init_ctimeEdit());
	layout->addStretch(1);
	layout->addLayout(init_okAndClose());
	widget()->setLayout(layout);
}

QWidget* ProductView::init_id()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("编号:"));
	m_idEdit = new QLineEdit();
	m_idEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^\\d+$"), m_idEdit));
	LeftLayout->addWidget(m_idEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::IdType,widget);
	//m_widgets[(int)Type::IdType] = widget;
	return widget;
}

QWidget* ProductView::init_name()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("名称:"));
	m_nameEdit = new QComboBox();
	LeftLayout->addWidget(m_nameEdit);
	m_nameEdit->setEditable(true);

	connect(m_nameEdit, &QComboBox::currentTextChanged, [=](const QString& text) {
		if (m_nameEdit->findText(text) != -1)
			emit requestTypeSignals(text);
		});


	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::NameType, widget);
	//m_widgets[(int)Type::NameType] = widget;
	return widget;
}

QWidget* ProductView::init_type()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("类型:"));
	m_typeEdit = new QComboBox();
	LeftLayout->addWidget(m_typeEdit,1);
	m_typeEdit->setEditable(true);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::TypeType, widget);
	//m_widgets[(int)Type::TypeType] = widget;
	return widget;
}

QWidget* ProductView::init_price()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("价格:"));
	m_priceEdit = new QLineEdit();
	m_priceEdit->setValidator(new QDoubleValidator(m_priceEdit));
	LeftLayout->addWidget(m_priceEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::PriceType, widget);
	//m_widgets[(int)Type::PriceType] = widget;
	return widget;
}

QWidget* ProductView::init_number()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("数量:"));
	m_numberEdit = new QSpinBox();
	/*m_numberEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^\\d+$"), m_numberEdit));*/
	m_numberEdit->setRange(1,INT_MAX);
	m_numberEdit->setWrapping(true);
	LeftLayout->addWidget(m_numberEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::NumberType, widget);
	//m_widgets[(int)Type::NumberType] = widget;
	return widget;
}

QWidget* ProductView::init_totalPrice()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("总价格:"));
	m_totalPriceEdit = new QLineEdit();
	m_totalPriceEdit->setValidator(new QDoubleValidator(m_totalPriceEdit));
	LeftLayout->addWidget(m_totalPriceEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::TotalPriceType, widget);
	return widget;
}

QWidget* ProductView::init_mtimeEdit()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	auto label = new QLabel("修改时间:", this);
	LeftLayout->addWidget(label, 0, Qt::AlignVCenter | Qt::AlignLeft);

	m_mtimeEdit = new QDateTimeEdit(this);//
	LeftLayout->addWidget(m_mtimeEdit);
	m_mtimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
	m_mtimeEdit->setCalendarPopup(true);
	//m_mtimeEdit->setStyleSheet("background-color: white;");
	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::MtimeType, widget);
	return widget;
}

QWidget* ProductView::init_ctimeEdit()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	auto label = new QLabel("创建时间:", this);
	LeftLayout->addWidget(label, 0, Qt::AlignVCenter | Qt::AlignLeft);

	m_ctimeEdit = new QDateTimeEdit(this);//
	LeftLayout->addWidget(m_ctimeEdit);
	m_ctimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
	m_ctimeEdit->setCalendarPopup(true);
	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::CtimeType, widget);
	//m_widgets[(int)Type::CtimeType] = widget;
	return widget;
}

